/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Graphics;

import Enums.Direction;
import Enums.bmpSize;

import Settings.*;
import GameLogic.*;
import Network.*;

import org.lwjgl.opengl.*;
import org.lwjgl.*;
import org.lwjgl.util.*;
import org.lwjgl.input.*;

import java.lang.*;
import java.util.Vector;
import java.io.*;

/**
 *
 * @author RikuXan
 */
public class Main
{
    //Displays-Klasse - verantwortlich für das Fenster
    private static Displays dsp;
    
    //Game-Variablen
    private static Game gm;
    //Timer für zeitliche Abläufe
    private static Timer time;
    private static float dTime = 0;
    private static float wTime = 0;
    private static float speed = 1;

    private static boolean gameStarted  = false;
    private static boolean renderHelper = true;
    private static boolean isOptions    = false;
    private static boolean isGraphics   = false;
    private static boolean isGameplay   = false;
    private static boolean isGameMenu  = false;
    private static boolean isMainMenu  = true;
    private static boolean isHighscore = false;
    private static boolean isSelectLevel = false;
    private static boolean isMultiplayer = false;

    private static int tpw = 0; //Anzahl der Tiles pro Beildschirmbreite
    private static int tph = 0; //Anzahl der Tiles pro Beildschirmhoehe
    private static int tilesize = 0; //Grösse der Tiles
    private static int bmpsize = 0; //Grösse der zum Rendern benutzten bmps
    private static int posX = 0; //xPosition der linken, oberen Ecke des Bildschirms
    private static int posY = 0; //yPosition der linken, oberen Ecke des Bildschirms

    private static String mapname;
    private static int level;
    private static int maxLevel = 5;
    private static boolean deadable = true;


    public static void main( String[] args )
    {
        init();
    }

    public Main()
    {
    }

    //Initialisierung der nöigen Objekte
    public static void init()
    {
        try
        {
            Settings.loadSettings();
            gm = new Game();
            dsp = new Displays();
            time = new Timer();
            Sprite.init();
            Keyboard.create();
            Keyboard.enableRepeatEvents( false );
            Mouse.create();

            gameInit();

            mainloop();
        }
        catch( Exception e )
        {
            Sys.alert( "Error" , "General Game Failure! ");
            e.printStackTrace();
        }
    }


    public static void gameInit()
    {
        //Settings.loadSettings(); //Einstellungen werden geladen

        if( Settings.gameplay == "OnTheRun")
        {
            level = Settings.levelOnTheRun;
            mapname = "OnTheRun";
            Settings.endscore = 100;//
        }
        else if( Settings.gameplay == "Standard")
        {
            level = Settings.levelStd;
            mapname = "Std";
            Settings.endscore = 9999;//
        }
        else if( Settings.gameplay == "Unlock")//
        {//
            level = Settings.maxLevel;//
            mapname = "Std";//
            Settings.endscore = 100;//
        }//
        else
        {
            level = 1;
            mapname = "Std";
        }

        //Settings.endscore = 100;

        //load specified map
        loadMap(); // map wird geladen

        gm.addPlayer(); // der Spieler wird hinzugefügt

        //alle Koepfe der Schlangen werden ihren Spawns zugewiesen
        for( int i = 0 ; i < gm.returnPlayerCount() ; i++ )
        {
            resetPlayer(i);
        }
    }

    static void loadMap()
    {
        gm.loadMap( mapname + level );
    }

    public static void setSpawns()
    {
        for( int i = 0 ; i < gm.returnPlayerCount() ; i++ )
        {
            gm.setPlayerHead( i , gm.returnSpawnAt(i).getHeight() , gm.returnSpawnAt(i).getWidth() );
        }
    }

    public static void exit()
    {
        if( dsp != null )
            dsp.destroy();
        Keyboard.destroy();
        Mouse.destroy();
        System.exit(0);
    }

    public static void resize()
    {
        if( dsp != null )
            dsp.destroy();
        Keyboard.destroy();
        Mouse.destroy();
    }

    public static void mainloop()
    {
        while( Settings.gameRuns )
        {
            //Zeitberechnung
            Timer.tick();
            dTime += time.getTime() - wTime;
            Timer.tick();
            wTime = time.getTime();
            Timer.tick();

            dsp.update();

            if( dsp.isActive() )
            {
                move();
                render();
            }
            else
            {
                Sleep(150);

                if( dsp.isVisible() || dsp.isDirty() )
                    render();
            }

            if ( dsp.isCloseRequested() )
                Settings.gameRuns = false;
        }
        exit();
    }    

    //Move-Funktion des Spiels
    public static void move()
    {
        while( Keyboard.next() )
        {
            int key = Keyboard.getEventKey();

            if( key == Keyboard.KEY_ESCAPE  && Keyboard.getEventKeyState() )
            {
                isGameMenu = !isGameMenu;
                gameStarted = false;
            }

            else if( ( key == Keyboard.KEY_RIGHT || key == Keyboard.KEY_D ) && Keyboard.getEventKeyState() )
            {
                gm.setSnakeDirection( Direction.EAST );
                gameStarted = true;
            }

            else if( ( key == Keyboard.KEY_LEFT || key == Keyboard.KEY_A ) && Keyboard.getEventKeyState() )
            {
                gm.setSnakeDirection( Direction.WEST );
                gameStarted = true;
            }

            else if( ( key == Keyboard.KEY_UP || key == Keyboard.KEY_W ) && Keyboard.getEventKeyState() )
            {
                gm.setSnakeDirection( Direction.NORTH );
                gameStarted = true;
            }

            else if( ( key == Keyboard.KEY_DOWN || key == Keyboard.KEY_S ) && Keyboard.getEventKeyState() )
            {
                gm.setSnakeDirection( Direction.SOUTH );
                gameStarted = true;
            }

            /*else if( key == Keyboard.KEY_P  && Keyboard.getEventKeyState() )
            {
                gameStarted = !gameStarted;
            }*/

            else if( key == Keyboard.KEY_Q  && Keyboard.getEventKeyState() )
            {
                renderHelper = !renderHelper;
            }

            /*else if( key == Keyboard.KEY_G  && Keyboard.getEventKeyState() )
            {
                deadable = !deadable;
            }*/

        }

        if( dTime > ( Settings.moveStd / speed ) && gameStarted )
        {
            gm.move();
            dTime %= ( Settings.moveStd / speed );
        }

        generateFood();
        snakesEat();


        gm.checkPlayerSize();

        //überprüft ob ein Spieler tot ist
        if(deadable)
        {
            for( int i = 0 ; i < gm.returnPlayerCount() ; i++ )
            {
                if( gm.playerIsDead(i) )
                {
                    resetPlayer(i);

                    gameStarted = false;
                }
            }
        }
        checkLevel();

        Settings.wTilesPerWidth += -Mouse.getDWheel() / 120;
    }
 
    //Render-Funktion des Spiels
    public static void render()
    {
        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glLoadIdentity();

        if( isMainMenu )
        {
            /*if( isGraphics )
                drawGraphics();
            else if( isSelectLevel )
                drawSelectLevel();
            else if( isGameplay )
                drawGameplay();
            else if( isOptions )
                drawOptions();
            else if( isHighscore )
                drawHighscore();
            else if( isMainMenu )
                drawMainMenue();*/

            if( isHighscore )//
                drawHighscore();//
            else if( isSelectLevel )//
                drawSelectLevel();//
            else if( isOptions )//
                drawOptions();//
            else if( isMainMenu )//
                drawMainMenue();//
        }
        else
        {
            renderData();
            drawCenteredMap();
            drawSnakes();
            drawFood();
            drawScore();

            if( isGameMenu )
                drawGameMenue();

            if( renderHelper )
                renderWayHelper();
        }

    }

    //Spieler wird zum Spawn zurueckgesetzt
    public static void resetPlayer( int playerNr )
    {
        if( Settings.highScore < gm.returnPlayerScore( playerNr ) )
        {
            Settings.highScore = gm.returnPlayerScore( playerNr );
            Settings.saveSettings();
        }

        gm.resetSnake( playerNr );
        gm.setPlayerHead( playerNr , gm.returnSpawnAt(playerNr).getHeight() , gm.returnSpawnAt(playerNr).getWidth() );
        gm.setSnakeLength( playerNr , Settings.startLength );
        gm.resetPlayerScore( playerNr );
    }

    //wichtige werde zum Rendern werden berechnet
    public static void renderData()
    {
        //position des Kopfes
        int headPosX = 0;
        int headPosY = 0;
        if( gm.playerAt(0) )
        {
            headPosX = gm.snakePosX(0,0);
            headPosY = gm.snakePosY(0,0);
        }

        if( Settings.wTilesPerWidth < 1 )
            Settings.wTilesPerWidth = 1;

        tilesize = Settings.wWidth / Settings.wTilesPerWidth;
        bmpsize = 0;
        for ( int i = Settings.wbmpSmallest ; i <= Settings.wbmpBiggest ; i *= 2 )
        {
            if ( bmpsize < tilesize )
                bmpsize = i;
        }

        //Anzahl der Tiles per Breite und Höhe
        tpw = Settings.wTilesPerWidth;
        tph = Settings.wHeight / tilesize;

        //Position des Bildschirms
        posX = headPosX - tpw / 2;
        if( posX <= 0 || tpw >= gm.mapWidth() )
            posX = 0;
        else if( posX + tpw > gm.mapWidth() )
        {
            posX = gm.mapWidth() - tpw;
            while( posX + tpw > gm.mapWidth() )
                posX--;
        }

        posY = headPosY - tph / 2;
        if( posY <= 0 || tph >= gm.mapHeight() )
            posY = 0;
        else if( posY + tph >= gm.mapHeight() )
        {
            posY = gm.mapHeight() - tph;
            while( posY + tph >= gm.mapHeight() )
                posY--;
        }
    }



    //die Karte wird auf den Schlangenkopf zentriert gerendert
    public static void drawCenteredMap()
    {
        for( int y = 0 ; y < gm.mapHeight() && y <= tph ; y++ )
        {
            for( int x = 0 ; x <= gm.mapWidth() && x <= tpw ; x++ )
            {
                Sprite.drawNewTexture( bmpsize + gm.mapReturnTexture( x + posX , y + posY ) , x * tilesize , y * tilesize, tilesize );
            }
        }

    }

    //die Schlangen werden gerendert
    public static void drawSnakes()
    {
        for ( int j = 0 ; j < gm.returnPlayerCount() ; j++ )
        {
            for ( int i = 0 ; i < gm.getSnakeLength(j) ; i++ )
            {
                Sprite.drawNewTexture( bmpsize + gm.snakeReturnTexture( j , i ) + ".bmp" , ( gm.snakePosX( j , i ) - posX ) * tilesize , ( gm.snakePosY( j , i ) - posY ) * tilesize , tilesize );
            }
            Sprite.drawNewTexture( bmpsize + gm.snakeReturnTexture( j , 0 ) + gm.getSnakeOrientation() + ".bmp" , ( gm.snakePosX( j , 0 ) - posX ) * tilesize , ( gm.snakePosY( j , 0 ) - posY ) * tilesize , tilesize );
        }
    }

    //die SNahrung wird gerendert
    public static void drawFood()
    {
        for ( int j = 0 ; j < gm.getTempsLength() ; j++ )
        {
                Sprite.drawNewTexture( bmpsize + gm.tempReturnTexture( j ) , ( gm.tempPosX( j ) - posX ) * tilesize , ( gm.tempPosY( j ) - posY ) * tilesize , tilesize );
        }
    }

    //die Punktzahl wird gerendert
    public static void drawScore()
    {
        int scoreLength = 4;
        int score[] = new int[scoreLength];
        for (int i = 0 ; i < scoreLength ; i++)
        {
            score[i]=gm.getScore(0);
            for(int k = 0 ; k < i ; k++)
            {
                score[i]/=10;
            }
            score[i]%=10;
        }
        for (int j = 0 ; j < scoreLength ; j ++)
        {
            Sprite.drawNewTexture("Numbers/" + bmpsize + "_" + score[j] + ".bmp", Settings.wWidth - tilesize*(j+1), 0, tilesize);
        }
    }

    //der Pfeil der den Weg zur naechsten Nahrung wird derendert
    public static void renderWayHelper()
    {
        for(int i = 0 ; i < gm.getTempsLength() ; i++)
        {
            if(gm.tempIsFood(i))
            {
                int foodPosX = gm.tempPosX(i);
                int foodPosY = gm.tempPosY(i);

                if(posY + tph <= foodPosY && posX + tpw <= foodPosX)
                {
                    Sprite.drawTriangle(new Dimension(Settings.wWidth,Settings.wHeight),
                            new Dimension(Settings.wWidth - tilesize + 1,Settings.wHeight - tilesize/2),
                            new Dimension(Settings.wWidth - tilesize/2,Settings.wHeight - tilesize + 1));
                }
                else if(posY + tph < foodPosY && posX > foodPosX)
                {
                    Sprite.drawTriangle(new Dimension(0,Settings.wHeight),
                            new Dimension(tilesize - 1,Settings.wHeight - tilesize/2),
                            new Dimension(tilesize/2,Settings.wHeight - tilesize + 1));
                }
                else if(posY >= foodPosY && posX + tpw <= foodPosX)
                {
                    Sprite.drawTriangle(new Dimension(Settings.wWidth,0),
                            new Dimension(Settings.wWidth - tilesize + 1,tilesize/2),
                            new Dimension(Settings.wWidth - tilesize/2,tilesize - 1));
                }
                else if(posY > foodPosY && posX > foodPosX)
                {
                    Sprite.drawTriangle(new Dimension(0,0),
                            new Dimension(tilesize - 1,tilesize/2),
                            new Dimension(tilesize/2,tilesize - 1));
                }
                else if(posY + tph < foodPosY)
                {
                    Sprite.drawTriangle(new Dimension((foodPosX - posX)*tilesize + tilesize/2,Settings.wHeight),
                            new Dimension((foodPosX - posX)*tilesize,Settings.wHeight - tilesize + 1),
                            new Dimension((foodPosX - posX)*tilesize + tilesize,Settings.wHeight - tilesize + 1));
                }
                else if(posY > foodPosY)
                {
                    Sprite.drawTriangle(new Dimension((foodPosX - posX)*tilesize + tilesize/2,0),
                            new Dimension((foodPosX - posX)*tilesize,tilesize + 1),
                            new Dimension((foodPosX - posX)*tilesize + tilesize,tilesize + 1));
                }
                else if(posX + tpw <= foodPosX)
                {
                    Sprite.drawTriangle(new Dimension(Settings.wWidth,(foodPosY - posY)*tilesize + tilesize/2),
                            new Dimension(Settings.wWidth - tilesize + 1,(foodPosY - posY)*tilesize),
                            new Dimension(Settings.wWidth - tilesize + 1,(foodPosY - posY)*tilesize+tilesize));
                }
                else if(posX > foodPosX)
                {
                    Sprite.drawTriangle(new Dimension(0,(foodPosY - posY)*tilesize + tilesize/2),
                            new Dimension(tilesize + 1,(foodPosY - posY)*tilesize),
                            new Dimension(tilesize + 1,(foodPosY - posY)*tilesize+tilesize));
                }
                else
                {

                }
            }
        }
    }

    //es wird überprüft ob sich die Schlangen auf Nahrung befindet und sie wird eventuell gefressen
    public static void snakesEat()
    {

        for ( int i = 0 ; i < gm.getTempsLength() ; i++ )
        {
            if( gm.tempIsFood(i) )
            {
                for ( int j = 0 ; j < gm.returnPlayerCount() ; j++ )
                {
                    if( gm.tempPosX(i) == gm.snakePosX( j , 0 ) && gm.tempPosY(i) == gm.snakePosY( j , 0 ) )
                    {
                        gm.snakeEat(j);
                        gm.removeTemp(i);
                    }

                }
            }
        }
    }

    //neue Nahrung wird erstellt falls noch nicht vorhanden
    public static void generateFood()
    {
        gm.generateFood();
    }

    public static void drawGameMenue()
    {
        Vector<String> vec = new Vector<String>();
        vec.add("Return to Game");
        vec.add("Restart");
        vec.add("toMainmenu");
        vec.add("Exit");

        Menu.drawMenu( vec );

        //System.out.println(mouseClickInMenu(vec));

        if( Mouse.isButtonDown(0) )
        {
            int i = mouseClickInMenu( vec );

            if( i == 3 )
            {
                isGameMenu = false;
                gameStarted = true;

                gameInit();
                Sleep(150);
            }
            else if( i == 2 )
            {
                resetPlayer(0);
                isGameMenu = false;
                gameStarted = false;

                Sleep(150);
            }
            else if( i == 1 )
            {
                isGameMenu = false;
                gameStarted = false;
                level = 1;
                isMainMenu = true;

                Sleep(150);
            }
            else if( i == 0 )
            {
                exit();
            }
        }
    }

    //die Punktzahl wird überprüft und gegebenenfalls das nächste Level gestartet
    public static void checkLevel()
    {
        if(gm.getScore(0) >= Settings.endscore)
        {
            level++;

            if(Settings.maxLevel <= level)
            {
                Settings.maxLevel = level;
                Settings.saveSettings();
            }

            if(level < maxLevel)
            {
                loadLevel(level);
                //gm.loadMap(mapname + level);
            }
            else
            {
                Settings.endscore+=1000;
                Sys.alert("Glueckwunsch!","Sie haben gewonnen!");
            }
            resetPlayer(0);
        }
    }
    
    public static void drawMainMenue()
    {
        Vector<String> vec = new Vector<String>();
        //vec.add("Start");
        vec.add("Select Level");
        vec.add("Unlock Level");
        vec.add("Options");
        //vec.add("Highscore");
        vec.add("Exit");

        Menu.drawMenu( vec );
        
        if( Mouse.isButtonDown(0) )
        {
            int i = mouseClickInMenu( vec );
            
            if( i == 3 )
            {
                /*isMainMenu = !isMainMenu;
                gm.clearTemps();
                resetPlayer(0);*/
                isSelectLevel = true;
                Sleep(150);
            }
            else if( i == 2 )
            {
                //isOptions = true;
                unlockLevel();

                Sleep(150);
            }
            else if( i == 1 )
            {
                //isHighscore = true;
                isOptions = true;//
                Sleep(150);
            }
            else if( i == 0 )
            {
                exit();
            }
        }

    }
    
    public static void drawOptions()
    {
        Vector<String> vec = new Vector<String>();
        //vec.add("Gameplay");
        //vec.add("Graphics");
        vec.add("Highscore");//
        vec.add("Reset");
        vec.add("Back");

        Menu.drawMenu( vec );

        if( Mouse.isButtonDown(0) )
        {
            int i = mouseClickInMenu(vec);

            if( i == 3 )
            {
                //isGameplay = true;


                Sleep(150);
            }
            else if( i == 2 )
            {
                //isGraphics = true;
                isHighscore = true;//
                Sleep(150);
            }
            else if( i == 1)
            {
                Settings.levelStd = 1;
                Settings.levelOnTheRun = 1;
                Settings.maxLevel = 1;
                Settings.gameplay = "Standard";
                Settings.scorePerFood = 10;
                Settings.scorePerMove = 0;
                Settings.highScore = 0;
                Settings.saveSettings();
            }
            else if( i == 0 )
            {
                isOptions = false;

                Sleep(150);
            }
        }
    }

    public static void drawMultiplayer()
    {
        Vector<String> vec = new Vector<String>();
        vec.add("Join Game");
        vec.add("Start Game");
        vec.add("Back");

        Menu.drawMenu(vec);
        if( Mouse.isButtonDown(0) )
        {
            int i = mouseClickInMenu(vec);

            if( i == 2 )
            {
                Sleep(150);
            }
            else if( i == 1 )
            {
                Sleep(150);
            }
            else if( i == 0 )
            {
                isMultiplayer = false;
                exit();
                Sleep(150);
            }
        }
    }
    
    public static void drawGameplay()
    {
        Vector<String> vec = new Vector<String>();
        vec.add("Standard");
        vec.add("On the Run");
        vec.add("Select Level");

        Menu.drawMenu( vec );

        if( Mouse.isButtonDown(0) )
        {
            int i = mouseClickInMenu(vec);

            if( i == 2 )
            {
                Settings.scorePerFood = 10;
                Settings.scorePerMove =  0;
                Settings.saveSettings();
                isGameplay = false;

                Sleep(150);
            }
            else if( i == 1 )
            {
                Settings.scorePerFood =  0;
                Settings.scorePerMove =  1;
                Settings.saveSettings();
                isGameplay = false;

                Sleep(150);
            }
            else if( i == 0 )
            {
                isSelectLevel= true;
                Sleep(150);
            }
        }
    }
    
    public static void drawGraphics()
    {
        Vector<String> vec = new Vector<String>();
        vec.add("600x400");
        vec.add("800x600");
        vec.add("1024x768");
        vec.add("1280x1024");

        Menu.drawMenu( vec );

        if( Mouse.isButtonDown(0) )
        {
            int i = mouseClickInMenu( vec );

            if( i == 3 )
            {
                Settings.wWidth  = 600;
                Settings.wHeight = 400;
                isGraphics = false;
                isOptions=false;
                Settings.saveSettings();

                resize();
                init();

                Sleep(150);
            }
            else if( i == 2 )
            {
                Settings.wWidth  = 800;
                Settings.wHeight = 600;
                isGraphics = false;
                isOptions=false;
                Settings.saveSettings();

                resize();
                init();

                Sleep(150);
            }
            else if( i == 1 )
            {
                Settings.wWidth  = 1024;
                Settings.wHeight =  768;
                isGraphics = false;
                isOptions=false;
                Settings.saveSettings();

                resize();
                init();

                Sleep(150);
            }
            else if( i == 0 )
            {
                Settings.wWidth  = 1280;
                Settings.wHeight = 1024;
                isGraphics = false;
                isOptions=false;
                Settings.saveSettings();

                resize();
                init();

                Sleep(150);
            }
        }
    }

    public static void drawHighscore()
    {
        Vector<String> vec = new Vector<String>();
        vec.add("max Level");
        vec.add("Highscore");
        vec.add("Back");

        Menu.drawMenu( vec );

        Sprite.drawNewTexture("Numbers/" + 32 + "_" + Settings.maxLevel%10 + ".bmp" , Settings.wWidth/2 + Menu.menuWidth/2 - 32 - bmpSize.BOARDER_SPACE.code , Settings.wHeight/2 - bmpSize.HEIGHT_MENU.code/2 + bmpSize.BOARDER_SPACE.code + 0 * (bmpSize.HEIGHT_MENU.code - 2*bmpSize.BOARDER_SPACE.code)/(vec.size()-1) - bmpSize.DEFAULT_HEIGHT.code/2 , 32 );
        Sprite.drawNewTexture("Numbers/" + 32 + "_" + Settings.maxLevel/10 + ".bmp" , Settings.wWidth/2 + Menu.menuWidth/2 - 64 - bmpSize.BOARDER_SPACE.code , Settings.wHeight/2 - bmpSize.HEIGHT_MENU.code/2 + bmpSize.BOARDER_SPACE.code + 0 * (bmpSize.HEIGHT_MENU.code - 2*bmpSize.BOARDER_SPACE.code)/(vec.size()-1) - bmpSize.DEFAULT_HEIGHT.code/2 , 32 );

        Sprite.drawNewTexture("Numbers/" + 32 + "_" + Settings.highScore      % 10 + ".bmp" , Settings.wWidth/2 + Menu.menuWidth/2 -  32 - bmpSize.BOARDER_SPACE.code , Settings.wHeight/2 - bmpSize.HEIGHT_MENU.code/2 + bmpSize.BOARDER_SPACE.code + 1 * (bmpSize.HEIGHT_MENU.code - 2*bmpSize.BOARDER_SPACE.code)/(vec.size()-1) - bmpSize.DEFAULT_HEIGHT.code/2 , 32 );
        Sprite.drawNewTexture("Numbers/" + 32 + "_" + Settings.highScore /10  % 10 + ".bmp" , Settings.wWidth/2 + Menu.menuWidth/2 -  64 - bmpSize.BOARDER_SPACE.code , Settings.wHeight/2 - bmpSize.HEIGHT_MENU.code/2 + bmpSize.BOARDER_SPACE.code + 1 * (bmpSize.HEIGHT_MENU.code - 2*bmpSize.BOARDER_SPACE.code)/(vec.size()-1) - bmpSize.DEFAULT_HEIGHT.code/2 , 32 );
        Sprite.drawNewTexture("Numbers/" + 32 + "_" + Settings.highScore /100 % 10 + ".bmp" , Settings.wWidth/2 + Menu.menuWidth/2 -  96 - bmpSize.BOARDER_SPACE.code , Settings.wHeight/2 - bmpSize.HEIGHT_MENU.code/2 + bmpSize.BOARDER_SPACE.code + 1 * (bmpSize.HEIGHT_MENU.code - 2*bmpSize.BOARDER_SPACE.code)/(vec.size()-1) - bmpSize.DEFAULT_HEIGHT.code/2 , 32 );
        Sprite.drawNewTexture("Numbers/" + 32 + "_" + Settings.highScore /1000     + ".bmp" , Settings.wWidth/2 + Menu.menuWidth/2 - 128 - bmpSize.BOARDER_SPACE.code , Settings.wHeight/2 - bmpSize.HEIGHT_MENU.code/2 + bmpSize.BOARDER_SPACE.code + 1 * (bmpSize.HEIGHT_MENU.code - 2*bmpSize.BOARDER_SPACE.code)/(vec.size()-1) - bmpSize.DEFAULT_HEIGHT.code/2 , 32 );

        if( Mouse.isButtonDown(0) )
        {
            int i = mouseClickInMenu(vec);

            if( i == 0 )
                isHighscore = false;

            Sleep(150);
        }
    }
    public static void drawSelectLevel()
    {
        Vector<String> vec = new Vector<String>();

        switch(Settings.maxLevel)
        {
            case 4:
                vec.add( "Numbers/32_4" );
            case 3:
                vec.add( "Numbers/32_3" );
            case 2:
                vec.add( "Numbers/32_2" );
            case 1:
                vec.add( "Numbers/32_1" );
                vec.add( "Back" );
                break;
        }
        
        //Verbesserung zur Switch-Anweisung oben, funktioniert nur irgendwie nicht
        /*String mapName;
        for(int i = Settings.maxLevel ; i > 0 ; --i )
        {
            mapName = "Numbers/32_" + i;
            vec.add( mapName );
            System.out.println(mapName);
        }
        vec.add("Back");*/

        if( Settings.maxLevel >= maxLevel )
        {
            vec.add( "Numbers/32_" + Settings.maxLevel );
            vec.add( "Numbers/32_" + Settings.maxLevel );
            vec.add( "Numbers/32_" + Settings.maxLevel );
            vec.add( "Numbers/32_" + Settings.maxLevel );
            vec.add( "Numbers/32_" + Settings.maxLevel );
            vec.add( "Back" );
        }

        Menu.drawMenu( vec );

        if( Mouse.isButtonDown(0) )
        {
            int i = mouseClickInMenu(vec);


            Settings.gameplay = "Standard";
            Settings.endscore = 9999;

            if( i == 5 )
            {
                Settings.gameplay = "OnTheRun";
            }
            else if( i == 4 )
            {
                Settings.gameplay = "OnTheRun";
            }
            
            isSelectLevel = false;
            loadLevel(i);
        }

    }

    public static void unlockLevel()
    {
        loadLevel( Settings.maxLevel );
    }
    
    private static void loadLevel( int i )
    {
        if( i > 0 && i <= maxLevel)
            level = i;

        
        switch(i)
        {
            case 5:
                Settings.gameplay = "OnTheRun";
                Settings.scorePerFood = 0;
                Settings.scorePerMove = 0;
                Settings.endscore = 9999;
                break;
            case 4:
                Settings.gameplay = "OnTheRun";
                Settings.scorePerFood = 50;
                Settings.scorePerMove = 1;
                Settings.endscore = 100;
                break;
            case 3:
            case 2:
            case 1:
                Settings.gameplay = "Standard";
                Settings.scorePerFood = 10;
                Settings.scorePerMove = 0;
                Settings.endscore = 250;
                break;
            default:
                Settings.gameplay = "Standard";
                Settings.scorePerFood = 10;
                Settings.scorePerMove = 0;
                Settings.endscore = 250;
        }

        isMainMenu = false;
        gm.clearTemps();
        resetPlayer(0);
        loadMap();
        Sleep(150);
    }

    public static int mouseClickInMenu( Vector<String> vec )
    {

        for( int i = 0 ; i < vec.size() ; ++i )
        {
            if( ( Mouse.getX() > Settings.wWidth/2 - Menu.returnWidth( vec.elementAt(i) ) ) &&
                ( Mouse.getX() < Settings.wWidth/2 + Menu.returnWidth( vec.elementAt(i) ) ) &&
                ( Mouse.getY() > Settings.wHeight/2 - bmpSize.HEIGHT_MENU.code/2 + bmpSize.BOARDER_SPACE.code + i * (bmpSize.HEIGHT_MENU.code - 2*bmpSize.BOARDER_SPACE.code)/(vec.size()-1) - bmpSize.DEFAULT_HEIGHT.code/2) &&
                ( Mouse.getY() < Settings.wHeight/2 - bmpSize.HEIGHT_MENU.code/2 + bmpSize.BOARDER_SPACE.code + i * (bmpSize.HEIGHT_MENU.code - 2*bmpSize.BOARDER_SPACE.code)/(vec.size()-1) + bmpSize.DEFAULT_HEIGHT.code/2)  )
            {
                return i;
            }
        }
        return -1;
    }

    public static void Sleep( int i )
    {
        try
        {
            Thread.sleep(i);
        }
        catch(Exception e )
        {
            System.out.print(e);

            Sys.alert("General GameFailure", e.toString());
        }
    }

    public static int returnBMPsize()
    {
        return bmpsize;
    }
}
